package com.hymjweb.demo.hymj.basic_data_manage.xt_upload_file.controller;


import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.util.FileUploadUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_upload_file.bean.XtUploadFilePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_upload_file.service.XtUploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.util.Map;

@Controller
@RequestMapping("/pages/basic_data_manage/xt_upload_file")
public class XtUploadFileController extends BaseController {

    @Autowired
    private XtUploadFileService xtUploadFileService;


    @RequestMapping("/init")
    public String init(HttpServletRequest request) {
        return "pages/basic_data_manage/xt_upload_file/xtuploadfile";
    }


    @RequestMapping("/showPictrue")
    public String showPictrue(String fileId){
        setRequestAttribute("fileId",fileId);
        return "/pages/basic_data_manage/xt_upload_file/showPictrue";
    }


    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(required = false) Map param) throws Exception {
        return xtUploadFileService.getPageList(param);
    }


    @RequestMapping("/filedownload")
    public void  filedownload(HttpServletRequest request,HttpServletResponse response){
        String fileName = request.getParameter("fileName");
        if(fileName!=null){
            File file = xtUploadFileService.getFile(fileName);
            FileUploadUtil.downLoadFile(request,response,file);
        } // end if fileName
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody(required = true) Map param){
        BaseResultMap baseResultMap = new BaseResultMap();
        if (param != null && !param.isEmpty()) {
            int flag = xtUploadFileService.delete(param);
            if (flag == SysConstant.OPERATE_STATUS.SUCCESS.getId()) {
                baseResultMap.setOk(true);
                baseResultMap.setMessage("删除文件" + SysConstant.OPERATE_STATUS.SUCCESS.getName() + "!");
            }
            else if(flag==SysConstant.OPERATE_STATUS.EXCEPTION_ID.getId()){
                baseResultMap.setOk(true);
                baseResultMap.setMessage("删除文件" + SysConstant.OPERATE_STATUS.EXCEPTION_ID.getName() + "!");
            }
            else if(flag==SysConstant.OPERATE_STATUS.FAILURE.getId()){
                baseResultMap.setOk(true);
                baseResultMap.setMessage("删除文件" + SysConstant.OPERATE_STATUS.FAILURE.getName() + "!");
            }

        } else {
            baseResultMap.setOk(true);
            baseResultMap.setMessage("删除文件" + SysConstant.OPERATE_STATUS.EXCEPTION_ARGS.getName() + "!");
        }
        return baseResultMap;
    }

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    @ResponseBody
    public Object upload(MultipartFile upLoadFileData) throws Exception {
        BaseResultMap baseResultMap = new BaseResultMap();
        if (upLoadFileData == null) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("文件获取失败!");
        } else {

            XtUploadFilePojo pojo = xtUploadFileService.upload(upLoadFileData);
            if(pojo!=null){
                baseResultMap.setOk(true);
                baseResultMap.setMessage(SysConstant.FILE_OPERATE_STATUS.UPLOAD_FILE_SUCCESS.getName());
                baseResultMap.setData(pojo.getId());
            }
            else{
                baseResultMap.setOk(false);
                baseResultMap.setMessage(SysConstant.FILE_OPERATE_STATUS.UPLOAD_FILE_FAILURE.getName());
            }
        }
        return baseResultMap;
    }

    @RequestMapping("/download")
    @ResponseBody
    public Object download(@RequestBody(required = true)Map param,HttpServletResponse response){
        BaseResultMap baseResultMap = new BaseResultMap();
        if (param == null) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("选择的文件可能已经被删除,下载失败！");
            return baseResultMap;
        }

        String id= DataTypeUtil.toNotNullString(param.get("id"));
        if(!DataTypeUtil.isEmptyStr(id)){
            XtUploadFilePojo pojo = xtUploadFileService.findObjectById(id);
            if(pojo!=null){
                File file = xtUploadFileService.getFile(pojo);
                if(file != null && file.exists()){
                    // 开始下载
                    baseResultMap.setOk(true);
                    baseResultMap.setMessage("选择的文件开始下载！");
                    baseResultMap.setData(pojo.getFileName());
                }
            }
        }

        return baseResultMap;
    }


    @RequestMapping("/show")
    public void show(String fileId,HttpServletResponse response) throws Exception{
        OutputStream outStream = null;
        XtUploadFilePojo pojo = xtUploadFileService.findObjectById(fileId);
        File file = xtUploadFileService.getFile(pojo);

        BufferedImage image = ImageIO.read(file);
        response.setContentType("image/png");
        outStream = response.getOutputStream();

        if(image != null){
            ImageIO.write(image,"png",outStream);
        }
    }

}
